﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public  class OCRService:IOCR
    { 
       
        public  List<string> GetImagePixel(Bitmap img)//过滤
        {
            //0 黑色
            //95 深灰
            //240 浅灰
            //255 白
            //128 0 0 目标颜色
            List<int> R = new List<int>();
            List<int> G = new List<int>();
            List<int> B = new List<int>();
            List<string> XORY = new List<string>();
            //byte[] result = new byte[img.Width * img.Height * 3];
            //int n = 0;
            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {
                    var a = img.GetPixel(x, y);
                    //if (img.GetPixel(x, y).R>=133 && img.GetPixel(x, y).R <= 159)
                    //{
                    //    if (img.GetPixel(x,y).G>=11&&img.GetPixel(x,y).G<=67)
                    //    {
                    //        if (img.GetPixel(x,y).B>=11&&img.GetPixel(x,y).B<=67)
                    //        {
                    if (a.R == 128 && a.G == 0 && a.B == 0)
                    {
                        R.Add(img.GetPixel(x, y).R);
                        G.Add(img.GetPixel(x, y).G);
                        B.Add(img.GetPixel(x, y).B);
                        XORY.Add(x + "|" + y);
                    }


                    //        }
                    //    }

                    //}


                    //result[n] = img.GetPixel(x,y).R;
                    //    result[n + 1] = img.GetPixel(x, y).G;
                    //    result[n + 2] = img.GetPixel(x,y).B;
                    //    n += 3;

                }
            }
            // insert(XORY);
            return XORY;
        }

        public Bitmap GetScreenSnapshot(int x, int y, int wid, int hei)//截图
        {
            Rectangle rectangle = new Rectangle(x, y, wid, hei);
            Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);
            }
            return bitmap;
        }

        public string TestOCRProperty(List<string> gaulxy, string gaulFile, string gaulKey)//图片识别
        {
            using (Bitmap bt = new Bitmap(@"" + gaulFile))
            {
                List<string> goal = new List<string>();//目标图片坐标
                List<string> read = gaulxy;//Read(@"C:\Users\Administrator\Desktop\XY\相对坐标\0相对坐标.txt");//标准坐标
                                           // Bitmap bt = new Bitmap(@""+ gaulFile);
                var data = GetImagePixel(bt);

                for (int i = 0; i < data.Count; i++)
                {
                    if (i != data.Count - 1)
                    {
                        string[] data1 = data[i].Split(new char[] { '|' });
                        string[] data2 = data[i + 1].Split(new char[] { '|' });
                        int a = Convert.ToInt32(data2[0]) - Convert.ToInt32(data1[0]);
                        int b = Convert.ToInt32(data2[1]) - Convert.ToInt32(data1[1]);
                        goal.Add(a + "|" + b);
                    }
                    else
                    {
                        string[] data2 = data[0].Split(new char[] { '|' });
                        string[] data1 = data[data.Count - 1].Split(new char[] { '|' });
                        int a = Convert.ToInt32(data2[0]) - Convert.ToInt32(data1[0]);
                        int b = Convert.ToInt32(data2[1]) - Convert.ToInt32(data1[1]);
                        goal.Add(a + "|" + b);
                    }


                }

                List<int> Equals = new List<int>();
                int result = -1;
                for (int i = 0; i < goal.Count; i++)
                {
                    if (i <= read.Count - 1)
                    {
                        result = -10;
                        result = String.Compare(goal[i], read[i]);
                        Equals.Add(result);
                    }

                }
                int resultdata = read.Count;
                int testdata = 0;
                for (int i = 0; i < Equals.Count; i++)
                {
                    if (Equals[i] == 0)
                    {
                        testdata += 1;
                    }
                }
                if (testdata >= resultdata * 90 / 100)
                {
                    // MessageBox.Show("成功");
                    return gaulKey;
                }
                return "-1";
            }
        }

      
    }
}
